﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using CMSWCSFSolution.CMS.Interface.Services;
using CMS.Model;
using CMS.Channel;
using CMSWCSFSolution.CMS.Views;


namespace CMSWCSFSolution.CMS.Views
{
    public class SaveCoursePresenter : Presenter<ISaveCourseView>
    {

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //       The code will not work in the Shell module, as a module controller is not created by default
        //
        // private ICMSController _controller;
        // public SaveCoursePresenter([CreateNew] ICMSController controller)
        // {
        // 		_controller = controller;
        // }
        private int _courseId;
        private ICMSService _service;
        
        public SaveCoursePresenter([CreateNew]ICMSService service)
        {
            _service = service;
        }


        public override void OnViewLoaded()
        {

            // TODO: Implement code that will be executed every time the view loads
        }

        public override void OnViewInitialized()
        {
            _courseId = View.CourseId;
            View.Teachers = _service.GetAllTeacher();
            View.Levels = _service.GetAllLevels();
            View.StudentsNotInCourse = _service.GetStudentsNotInCourse(_courseId);
            View.StudentsInCourse = _service.GetStudentsInCourse(_courseId);
            if (_courseId > 0)
            {
                View.Courses = _service.GetCourseById(_courseId);
            }

            // TODO: Implement code that will be executed the first time the view loads
        }
        public void DeleteAllStudentsByCourseId(int courseId)
        {
            _service.DeleteAllStudentsByCourseId(courseId);
        }
        public int AddCourse(Course course)
        {
            return _service.AddCourse(course);
        }
        public void UpdateCourse(Course course)
        {
            _service.UpdateCourse(course);
        }
        public void AddCourseDetail(CourseDetails courseDetail)
        {
            _service.AddCourseDetail(courseDetail);
        }
        public void SavePage(int ID)
        {
            
            if (View.LevelIndex > 0 && View.TeacherIndex > 0)
            {
                int courseId;
                Course course = new Course();
                course.CourseName = View.CourseName;
                course.TeacherId = View.TeacherId;
                course.LevelId = View.LevelId;
                course.CourseStatus = View.Status;

                if (ID < 0)
                {
                    courseId = AddCourse(course);
                }
                else
                {
                    course.CourseId = ID;
                    UpdateCourse(course);
                    courseId = ID;
                }
                for (int i = 0; i < View.Count; i++)
                {
                    CourseDetails courseDetail = new CourseDetails();
                    courseDetail.CourseId = courseId;
                    courseDetail.StudentId = View.StudentIdInSelectedList[i];
                    AddCourseDetail(courseDetail);
                }
                
            }

        }

    }




    // TODO: Handle other view events and set state in the view
}





